package com.geese.module.common.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.geese.common.BaseController;
import com.geese.common.entity.School;
import com.geese.common.util.BeanUtil;
import com.geese.module.ceo.service.SchoolService;
import com.geese.module.common.dto.SchoolDto;
import com.geese.module.user.service.UserService;
import com.google.common.collect.ImmutableMap;

import ma.glasnost.orika.metadata.Type;

/**
 * @author YeFangYi 
 */
@Controller
@RequestMapping(value = "/school")
@Scope("prototype")
public class SchoolCommonController extends BaseController {

    @Autowired
    private SchoolService schoolService;
    @Autowired
    private UserService userService;


    /**
     * 注册页获取全部学校，无需校验token
     */
    @GetMapping("/register/listAll")
    @ResponseBody
    public String schoolList() {
    	List<School> schoolList = schoolService.listAll();
    	Type<School> soureceType = BeanUtil.getType(School.class);
		Type<SchoolDto> targetType = BeanUtil.getType(SchoolDto.class);
		return response(ImmutableMap.of("list", BeanUtil.mapList(schoolList, soureceType, targetType)));
    }
    
    /**
     * 获取全部学校
     */
    @GetMapping("/listAll")
    @ResponseBody
    public String schoolList(@RequestParam String token) {
    	userService.checkUser(token);
    	
    	List<School> schoolList = schoolService.listAll();
    	Type<School> soureceType = BeanUtil.getType(School.class);
		Type<SchoolDto> targetType = BeanUtil.getType(SchoolDto.class);
		return response(ImmutableMap.of("list", BeanUtil.mapList(schoolList, soureceType, targetType)));
    }
    
}
